#!/bin/sh
#
# A hook script to verify what is about to be committed.
# Called by "git commit --amend" or "git rebase". The hook exits with
# non-zero status and warning messages if the files being rewritten do
# not conform to the Mesos style.
#
# To enable this hook, do this from the root of the repo:
#
# $ ln -s ../../support/hooks/post_rewrite .git/hooks/post_rewrite

# Redirect output to stderr.
exec 1>&2

# If there are whitespace errors, print the offending file names.
## In git, '@~' represent previous commit. We check the whitespace between
## current head and previous commit after a commit is rewritten.
git diff-index --check @~ -- || exit 1

# Check Mesos style.
## In git, '@' represent current head, '@~' represent previous commit. We check
## the style of changes between current head and previous commit after a commit
## is rewritten.
ADDED_OR_MODIFIED=$(git diff --name-only --diff-filter=AM @~..@)
if [ "$ADDED_OR_MODIFIED" ]; then
    # NOTE: We choose to implement this as a conditional rather than a call to
    # `xargs` on purpose. Some implementations of `xargs` will call your script
    # even if the arguments you pass in are empty. In our case, this would
    # cause us to erroneously lint every file in the repository. Additionally,
    # many implementations do not support the `-r` flag, (which instructs
    # `xargs` to not run the script if the arguments are empty), so we also
    # cannot use that.
    ./support/mesos-style.py $ADDED_OR_MODIFIED || exit 1
fi

# Check that the commits are properly split between mesos, libprocess and stout.
## In git, '@' represent current head, '@~' represent previous commit. We check
## the style of changes between current head and previous commit after a commit
## is rewritten.
git diff --name-only --diff-filter=AMD @~..@ | xargs ./support/mesos-split.py || exit 1
